//
//  server_send_reply.cpp
// ~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2013-2014 Razvan Coca (razvan dot coca at  gmail dot com)
// This is mostly generated code. Generator is distributed under GPL. Edit it to suit your needs.
// 



#include "server_send_reply.hpp"
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>

#include "patterns/command.hpp"
#include "patterns/handlers.hpp"

#include "logging/logging.hpp"

namespace pcmd=patterns::command;
namespace phdl=patterns::handlers;
namespace ba=boost::asio;
namespace bs=boost::system;
namespace bt=boost::posix_time;

#include "components/defs/util_types.hpp"
#include "components/defs/server_processor.hpp"
#include "components/protocol/server_send_reply.hpp"
namespace components{
namespace protocol{


bool server_send_reply::cancel()
{

    try{
        bool retcode=true;
        LOG(WARNING)<<name_<<"::"<<__FUNCTION__;
        retcode &= (create_enc_service_reply_ && create_enc_service_reply_->cancel());
        retcode &= (send_enc_pb_message_ && send_enc_pb_message_->cancel());
        op_create_enc_service_reply=nullptr;
        return retcode;
    }
    catch(std::exception &e){
        LOG(WARNING)<<name_<<"::"<<__FUNCTION__<<":"<<e.what();
        return false;
    }


}

boost::shared_ptr<server_send_reply> server_send_reply::create(ba::io_service& ios,crypto_buf_t& iv,crypto_buf_t& key,stats_message_server_processor& message_processor,ba::ip::udp::socket& msocket,udp_endpoint_t& mgroup)
{

    return boost::shared_ptr<server_send_reply>(new server_send_reply(ios,iv,key,message_processor,msocket,mgroup));
}

void server_send_reply::handle_create_enc_service_reply(const crypto_error& eec)
{


    LOG(INFO)<<name_<<"::"<<__FUNCTION__<<"["<<eec.GetWhat()<<"]";

    if(!phdl::is_error(eec))
    {

    }

    //send_enc_pb_message_ && send_enc_pb_message_->cancel();


}

void server_send_reply::handle_send_enc_pb_message(const bs::error_code& ec,std::size_t bytes_transferred)
{


    LOG(INFO)<<name_<<"::"<<__FUNCTION__<<"["<<ec<<"]";

    if(!ec)
    {
        LOG(INFO)<<__FUNCTION__<<":ec:\t"<<ec ;
        LOG(INFO)<<__FUNCTION__<<":bytes_transferred:\t"<<bytes_transferred ;

        if(!pcmd::invoke(op_create_enc_service_reply))
            cancel();
        return;
    }
    cancel();
    //create_enc_service_reply_ && create_enc_service_reply_->cancel();


}

void server_send_reply::init_components()
{

    if(!create_enc_service_reply_) create_enc_service_reply_ = components::protocol::create_enc_service_reply::create(ios_,iv_,key_,message_processor_);
    if(!send_enc_pb_message_) send_enc_pb_message_ = components::network::send_enc_pb_message::create(ios_,msocket_,mgroup_);
}

server_send_reply::server_send_reply(ba::io_service& ios,crypto_buf_t& iv,crypto_buf_t& key,stats_message_server_processor& message_processor,ba::ip::udp::socket& msocket,udp_endpoint_t& mgroup)
    :ios_(ios), iv_(iv), key_(key), message_processor_(message_processor), msocket_(msocket), mgroup_(mgroup)
{

    name_=std::string(__FUNCTION__);
    DLOG(INFO)<<name_<<"()";
    init_components();
}

server_send_reply::~server_send_reply()
{
    DLOG(INFO)<<name_<<"::"<<__FUNCTION__<<std::endl;
}
}//end namespace protocol
}//end namespace components

